home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTKEYCMP.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  63 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btkeycmp.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btkeycmp - compare btree keys
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btkeycmp(btp, buf1, buf2)
  20.      btree_t *btp;
  21.      const void *buf1;
  22.      const void *buf2;
  23.  
  24. DESCRIPTION
  25.      The btkeycmp function compares the keys pointed to be buf1 and
  26.      buf2.  buf1 and buf2 must point to keys of the size stored in
  27.      btree btp.  The value returned will be less than, equal to, or
  28.      greater than 0, according as the key pointed to by buf1 is less
  29.      than, equal to, or greater than the key pointed to by buf2.
  30.  
  31.      btkeycmp will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       btp is not a valid btree pointer.
  34.      [EINVAL]       buf is the NULL pointer.
  35.  
  36. SEE ALSO
  37.      btsearch.
  38.  
  39. ------------------------------------------------------------------------------*/
  40. int btkeycmp(btp, buf1, buf2)
  41. btree_t *btp;
  42. const void *buf1;
  43. const void *buf2;
  44. {
  45.     int    cmp    = 0;        /* result of key comparison */
  46.     int    fld    = 0;        /* field number */
  47.  
  48.     /* compare each field */
  49.     for (fld = 0; fld < btp->fldc; ++fld) {
  50.         cmp = (*btp->fldv[fld].cmp)((char *)buf1 + btp->fldv[fld].offset,
  51.             (char *)buf2 + btp->fldv[fld].offset, btp->fldv[fld].len);
  52.         if (cmp != 0) {
  53.             if (btp->fldv[fld].flags & BT_FDSC) {
  54.                 cmp = -cmp;
  55.             }
  56.             break;
  57.         }
  58.     }
  59.  
  60.     errno = 0;
  61.     return cmp;
  62. }
  63.